package com.zijin.service.user.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijin.PageQureyDTO;
import com.zijin.constant.ErrorMsgConstant;
import com.zijin.context.BaseContext;
import com.zijin.dto.user.RoleDTO;
import com.zijin.entity.user.Role;
import com.zijin.exception.BaseException;
import com.zijin.mapper.RoleMapper;
import com.zijin.result.PageResult;
import com.zijin.service.user.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zijin.vo.user.RoleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author JWei Xu
 * @since 2025-02-01
 */
@Slf4j
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Override
    public Set<String> getRoleName(Long id) {
        return baseMapper.getRoleName(id);
    }

    @Override
    public Set<String> getRoleNameByUserId(Long userId) {
        return baseMapper.getRoleName(userId);
    }

    @Override
    public Boolean addOne(RoleDTO dto) {
        List<Role> roles = this.lambdaQuery().and(w -> {
            w.or(w1 -> w1.eq(Role::getRoleKey, dto.getRoleKey()));
            w.or(w1 -> w1.eq(Role::getRoleName, dto.getRoleName()));
        }).list();
        if(ObjectUtil.isNotEmpty(roles)){
            throw new BaseException(ErrorMsgConstant.KEYWORD_REPETITION+"角色名或角色key");
        }
        Role role = BeanUtil.copyProperties(dto, Role.class);
        role.setId(null);
        role.setCreateTime(LocalDateTime.now());
        return this.save(role);
    }

    @Override
    public Boolean delOne(Long id) {
        this.checkById(id);
        return this.removeById(id);
    }

    @Override
    public Boolean editOne(Long id, RoleDTO dto) {
        this.checkById(id);
        Role role = BeanUtil.copyProperties(dto, Role.class);
        role.setId(id);
        role.setRoleKey(null);
        return this.updateById(role);
    }

    @Override
    public Role getRole(Long id) {
        this.checkById(id);
        return this.getById(id);
    }

    @Override
    public PageResult getPage(PageQureyDTO dto) {
        Page<Role> page = this.lambdaQuery()
                .or(ObjectUtil.isNotEmpty(dto.getSearch()),w -> w.like(Role::getRoleName, dto.getSearch()))
                .or(ObjectUtil.isNotEmpty(dto.getSearch()),w -> w.like(Role::getRemark, dto.getSearch()))
                .or(ObjectUtil.isNotEmpty(dto.getSearch()),w -> w.like(Role::getRoleKey, dto.getSearch()))
                .page(new Page<>(dto.getPageNum(), dto.getPageSize()));
        log.info(page.getRecords().toString());
        return new PageResult(page.getTotal(),page.getRecords());
    }

    @Override
    public List<RoleVo> getList(PageQureyDTO dto) {
        return BeanUtil.copyToList(this.lambdaQuery()
                .or(ObjectUtil.isNotEmpty(dto.getSearch()),w -> w.like(Role::getRoleName, dto.getSearch()))
                .or(ObjectUtil.isNotEmpty(dto.getSearch()),w -> w.like(Role::getRemark, dto.getSearch()))
                .or(ObjectUtil.isNotEmpty(dto.getSearch()),w -> w.like(Role::getRoleKey, dto.getSearch()))
                .list(),RoleVo.class);

    }

    /** 判断这个ID有没有对应的数据，如果没有，那么就抛出异常
     * 操作超级管理员数据也会抛出异常 */
    private void checkById(Long id){
        Role role = this.getById(id);
        if(ObjectUtil.isEmpty(role)){
            throw new BaseException(ErrorMsgConstant.DATA_NOT_FOUN);
        }
        if(ObjectUtil.equal("admin",role.getRoleName())){
            throw new BaseException(ErrorMsgConstant.ADMIN_NOT_ALLOWED);
        }
    }
}
